package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ProductCategoryDaoImpl;
import com.ysd.entity.ProductCategory;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminProductCategoryServlet extends BaseServlet {
	
	/**
	 *查询产品所有分类
	 *localhost:8080/kouhong/adminProductCategory?reqName=getProductCates
	 */
	public void getProductCates(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();//创建实现类对象
		int page = 0;
		if(request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		int size = 0;
		if(request.getParameter("size")!=null && !request.getParameter("size").equals("")) {
			size = Integer.parseInt(request.getParameter("size")); 
		}
		
		List<ProductCategory> pcList =  pcDao.getProductCategoryByPage(page, size);//分页查的产品分类

		//发送ajax请求,服务器都会返回gson格式字符串
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者

		Gson gson = new Gson();
		String res = gson.toJson(pcList);
		out.print(res);
		out.flush();
		out.close();
	}
	
	/**
	 *分页
	 *localhost:8080/kouhong/adminProductCategory?reqName=getProductByPage
	 */
	public void getProductByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();//创建实现类对象
		
		List<ProductCategory> pcList =  pcDao.getProductCategory();//分页查的产品分类

		//发送ajax请求,服务器都会返回gson格式字符串
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者

		Gson gson = new Gson();
		String res = gson.toJson(pcList.size());
		out.print(res);
		out.flush();
		out.close();
	}
	

	/**
	 *根据根据分类id删除产品分类
	 *localhost:8080/kouhong/adminProductCategory?reqName=delProductCate
	 */
	
	public void delProductCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int dpId = Integer.parseInt(request.getParameter("productCategoryId"));//接收前台传来的参数
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();//创建实现类对象
		int n = pcDao.deleteProductCateById(dpId);//调用删除的方法
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
		
		if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
	}


	/**
	 *添加
	 *localhost:8080/kouhong/adminProductCategory?reqName=addProductCate
	 */
	
	public void addProductCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
	
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();//创建实现类对象
		String productCategoryName = request.getParameter("productCategoryName");
		if(productCategoryName!=null && !productCategoryName.equals("")) {
			List<ProductCategory> pcList = pcDao.addProductCate(productCategoryName);
			if (pcList.size()==0) {//没有查到可以添加
				ProductCategory pc = new ProductCategory(0, productCategoryName);	
				int n = pcDao.addProductCategory(pc);
				if (n>0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {//查到了,不可以添加
				out.print(Result.toClient(false, "分类名称已存在", ""));
			}
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 *根据根据分类id查询产品分类
	 *localhost:8080/kouhong/adminProductCategory?reqName=getProductCateById
	 */
	
	public void getProductCateById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();//创建实现类对象
		int cId =0;
		if(request.getParameter("cateId")!=null && !request.getParameter("cateId").equals("")) {
			cId = Integer.parseInt(request.getParameter("cateId"));//调用实现类根据产品分类id查询产品
		}
		//String productCategoryName = request.getParameter("productCategoryName");接收前台传来的参数,兵装化为String类型
		ProductCategory pc = pcDao.getProductById(cId);
		
		Gson gson = new Gson();
		String res = gson.toJson(pc);//讲pc转化为json字符串
		out.print(res);//响应数据
		out.flush();
		out.close();
		
		
		}
	
	/**
	 *根据根据产品分类修改产品名称
	 *localhost:8080/kouhong/adminProductCategory?reqName=updateProductCateById
	 */
	
	public void updateProductCateById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();//创建实现类对象
		int productCategoryId=0;
		if(request.getParameter("cateId")!=null && !request.getParameter("cateId").equals("")) {
			 productCategoryId = Integer.parseInt(request.getParameter("cateId"));
		}
		String productCategoryName = request.getParameter("cateName");//接收前台传来的参数,兵装化为String类型
		ProductCategory pc = new ProductCategory(productCategoryId, productCategoryName);
		System.out.println(pc);
		int n = pcDao.updateProcateById(pc);
		if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		out.flush();
		out.close();
		
		
		}
	
	/**
	 *localhost:8080/kouhong/adminProductCategory?reqName=deleteProById
	 */
	
	public void deleteProById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();//创建实现类对象
		String proId = request.getParameter("proId");
		int n = pcDao.deleteProductAll(proId);//调用删除的方法
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
		
		if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
	}

	
	
}


